<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? $t("sys.new_API") : $t("sys.edit_API") }}</span>
        <span class="el-icon-close g9 f26 poi" @click="handleCancel"></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="120px">
      <el-form-item
        :label="$t('sys.apply_name')"
        prop="thirdAppName"
        :rules="[
          { required: true, message: $t('sys.apply_name_not_null'), trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.thirdAppName"
          style="width: 400px"
          :placeholder="$t('sys.enter_apply_name')"
          :maxlength="50"
          show-word-limit
        />
      </el-form-item>
      <el-form-item v-if="!isNew" label="AppId" prop="appId">
        <el-input
          v-model="formData.appId"
          disabled
          style="width: 400px"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>
      <el-form-item v-if="!isNew" label="AppSecret" prop="secretKey">
        <el-input
          v-model="formData.secretKey"
          disabled
          style="width: 400px"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="handleCancel">{{$t('sys.cancel')}}</el-button>
      <el-button type="primary" @click="submit">{{$t('sys.confirm')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postApiauthconfig,putApiauthconfig } from '@/apis/paas/HmApiAuthConfigController.js'
export default {
  name: "APIAuthConfigForm",
  data() {
    return {
      showVisible: false,
      formData: {},
    };
  },

  computed: {
    isNew() {
      return !this.formData.id;
    },
  },

  created() {},

  methods: {
    addForm(record = {}) {
      this.showVisible = true;
      this.formData = {
        ...record,
      };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    editForm(record = {}) {
      this.showVisible = true;
      this.formData = {
        ...record,
      };

      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    async submit() {
      try {
        await this.$refs.form.validate();
        const { id, thirdAppName } = this.formData;
        let params = {};
        let request = null;
        if (this.isNew) {
          params = { thirdAppName };
          request = postApiauthconfig;
        } else {
          params = {
            id,
            thirdAppName,
          };
          request = putApiauthconfig;
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === "string" ? params[key].trim() : params[key];
        }
        request(params).then((res) => {
          this.handleCancel();
          this.$emit("ok");
        });
      } catch (error) {
        console.log(error);
      }
    },
    handleCancel() {
      this.showVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
